package com.sunyard.utils;

import com.google.common.base.Preconditions;
import org.apache.commons.beanutils.BeanUtils;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;


/**
 * @author k3887
 * @Program DtoUtils
 * @Description TODO
 * @Date 2018/12/3
 * @Version V1.0
 */
public class DtoUtils {
    /**
     * @param to   数据去向
     * @param from 数据来源
     * @throws Exception
     * @Description
     */
    public static <T> void populateBeanToBean(T to, T from) throws Exception {

        /**
         * Description
         */
        Preconditions.checkNotNull(to);
        /**
         * Description
         */
        Preconditions.checkNotNull(from);

        /**
         * Description
         */
        BeanUtils.copyProperties(to, from);
    }

    /**
     * @param to   数据去向
     * @param from 数据来源
     * @throws Exception
     * @Description
     */
    public static <T> void cloneBeanToBean(T to, T from) throws Exception {

        Preconditions.checkNotNull(to);

        Preconditions.checkNotNull(from);

        ModelMapper mapper = new ModelMapper();
        mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        mapper.map(from, to);
    }
}
